package com.mx.recycleview.anytype

import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 * 单行显示的Adapt
 */
abstract class SingleLineAdapt : BaseTypeAdapt<Any>(arrayListOf()) {
    final override fun getSpanSize(): Int {
        return Int.MAX_VALUE
    }

    override fun getItemCount(): Int {
        if (!isCurrentAdaptShow) return 0
        showWidthAdapt?.let {
            if (it.itemCount <= 0) return 0
        }
        showIfAdaptEmpty?.let {
            if (it.itemCount > 0) return 0
        }
        return 1
    }

    override fun getItem(position: Int): Any? {
        return Any()
    }

    override fun bindView(position: Int, itemView: View, record: Any) {
        bindView(itemView)
    }

    abstract fun bindView(itemView: View)


    private var showWidthAdapt: BaseTypeAdapt<*>? = null
    private var showIfAdaptEmpty: BaseTypeAdapt<*>? = null

    protected var isCurrentAdaptShow = true
    fun setShow(show: Boolean) {
        isCurrentAdaptShow = show
    }

    /**
     * 当adapt的数量=0时，当前也会隐藏
     */
    fun showWithAdapt(adapt: BaseTypeAdapt<*>?) {
        showWidthAdapt = adapt
        adapt?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
            override fun onChanged() {
                this@SingleLineAdapt.notifyItemChanged(0)
            }
        })
    }

    /**
     * 当adapt的数量 > 0 时，当前会隐藏
     */
    fun showIfAdaptEmpty(adapt: BaseTypeAdapt<*>?) {
        showIfAdaptEmpty = adapt
        adapt?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
            override fun onChanged() {
                this@SingleLineAdapt.notifyItemChanged(0)
            }
        })
    }
}